package com.junjun.ssm.service.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.junjun.ssm.dao.MeiTuanDao;
import com.junjun.ssm.dto.OrderDto;
import com.junjun.ssm.entity.MovieEntity;
import com.junjun.ssm.entity.OrderEntity;
import com.junjun.ssm.service.IMeiTuanService;
import com.junjun.ssm.service.IOrderService;
import com.junjun.ssm.service.ToMovieService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MeiTuanServiceImpl implements IMeiTuanService {

    @Autowired
    MeiTuanDao meiTuanDao;

    @Autowired
    ToMovieService toMovieService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    IOrderService orderService;
 /*   //调用回调函数
    RabbitTemplate.ConfirmCallback callback = (CorrelationData correlationData, boolean bl, String srt) -> {
        if (bl) {
            OrderEntity orderEntity = JSON.parseObject(correlationData.getId(), OrderEntity.class);
            orderEntity.setState(1);
            orderService.save(orderEntity);

        }
    };*/

    @Override
    public List<MovieEntity> query() {

        List<MovieEntity> list = (List<MovieEntity>) meiTuanDao.findAll();

        return list;
    }

    public void addMeiTuan() {
        meiTuanDao.deleteAll();
        List<MovieEntity> movieEntityList = toMovieService.queryAllMovie();
        for (MovieEntity movieEntity : movieEntityList) {
            meiTuanDao.save(movieEntity);
        }
    }


    @HystrixCommand(fallbackMethod = "queryText2")
    public  String queryText1(){
     return    toMovieService.queryText();
    }
    public  String queryText2(){
        return    "出错啦";
    }

/*    @SneakyThrows//绑定回调函数
    public void send(OrderDto orderDto, String json) {
        //开启回调机制
        rabbitTemplate.setMandatory(true);
        //绑定回调函数
        rabbitTemplate.setConfirmCallback(callback);

        String orderId = String.valueOf(json);
        //创建关联数据
        CorrelationData c = new CorrelationData(orderId);

        rabbitTemplate.convertAndSend("directExchange", "movie.order", orderDto, c);

        Thread.sleep(200);

    }*/


}
